//
//  BIZOfficalVIPConsultDetailVC.m
//  biz
//
//  Created by zhangfuwei on 2017/7/10.
//  Copyright © 2017年 jieshuibao. All rights reserved.
//

#import "BIZOfficalVIPConsultDetailVC.h"
#import "BIZAddQuetionVC.h"

//cell
#import "BIZOfficialRapidConsultDetailQuestionCell.h"
#import "BIZOfficialAppendQuestionCell.h"
#import "BIZOfficialAnswerCell.h"
//model
#import "BIZConsultRecordModel.h"

@interface BIZOfficalVIPConsultDetailVC ()<UITableViewDataSource,UITableViewDelegate>
/** tableView */
@property (nonatomic, strong) UITableView *tableView;

@end

@implementation BIZOfficalVIPConsultDetailVC{
    BIZOfficialRapidConsultDetailModel *_detailModel;
    NSString    *_uuid;
    NSMutableDictionary    *_heightsDic;
}

-(instancetype)initWithUUID:(NSString *)uuid {
    self = [super init];
    if (self) {
        _uuid = uuid;
        _heightsDic = [NSMutableDictionary dictionary];
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(officialAnswerCellIndex:) name:kOfficialConsultDetailNotification object:nil];
    [self initSubViews];
    [_tableView.mj_header beginRefreshing];
}

- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)initSubViews {
    self.title = @"咨询详情";
    [self.view addSubview:self.tableView];
}

#pragma mark -- requset
- (void)requestOfficialConsultDetail {
    
    [RequestTool POST:kOfficialRapidConsultDetailURL
           parameters:@{@"uuid":_uuid}
             progress:nil
              success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
                  [_tableView.mj_header endRefreshing];
                  
                  DeLog(@"官方快速咨询(详情)--%@",responseObject);
                  _detailModel = [BIZOfficialRapidConsultDetailModel yy_modelWithJSON:responseObject];
                  
                  if (_detailModel.code.intValue == 1) {
                      [_tableView reloadData];
                  }else if (_detailModel.code.intValue == 2){
                      [MBProgressHUD showTipMessageInView:@"请登录"];
                      [BIZUserModel shareUser].login = NO;
                      [self presentViewController:[BIZLoginViewController viewControllerFromStoryboard] animated:YES completion:nil];
                      
                  }else {
                      [MBProgressHUD showErrorMessage:_detailModel.des];
                      
                  }
              }
              failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
                  [_tableView.mj_header endRefreshing];
                  [MBProgressHUD showErrorMessage:@"网络不给力,请检测网络" timer:2.5];
                  DeLog(@"error ---%@",error);
                  
              }
     ];
}

#pragma mark -- tableView数据源&& 代理

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _detailModel.data.stage.integerValue + 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    
    if (indexPath.row % 2 == 0) {
        if (!indexPath.row) {
            BIZOfficialRapidConsultDetailQuestionCell *cell = [BIZOfficialRapidConsultDetailQuestionCell cellWithTableView:tableView indexPath:indexPath];
            cell.questionModel = _detailModel.data;
            return cell;
        }
        BIZOfficialAppendQuestionCell *cell = [BIZOfficialAppendQuestionCell cellWithTableView:tableView indexPath:indexPath];
        cell.appendQuestionModel = _detailModel.data;
        return cell;
        
    }else {
        
        MJWeakSelf;
        BIZOfficialAnswerCell *cell = [BIZOfficialAnswerCell cellWithTableView:tableView indexPath:indexPath appendQusetion:^{
            BIZAddQuetionVC *appendVC = [[BIZAddQuetionVC alloc] initWithUUID:_detailModel.data.uuid
                                                                          url:kOfficialRapidConsultAppendURL
                                                                   completion:^{
                                                                       [weakSelf requestOfficialConsultDetail];
                                                                   }];
            
            [weakSelf.navigationController presentViewController:[[UINavigationController alloc] initWithRootViewController:appendVC] animated:YES completion:nil];
        }];
        NSNumber *height = [_heightsDic valueForKey:[NSString stringWithFormat:@"%ld",indexPath.row]];
        
        if (height) {
            DeLog(@"缓存idx--%ld",indexPath.row);
        }
        // MARK: 注意此处的缓存高度必须要再设置模型之前

        cell.cacheHeight = height !=nil ? height.floatValue:0;
        cell.answerModel = _detailModel.data;
        return cell;
        
    }
}

- (void)officialAnswerCellIndex:(NSNotification *)noti {
    [self.tableView reloadData];
    NSIndexPath *indexPath = noti.userInfo[@"indexPath"];
    NSNumber    *height    = noti.userInfo[@"newH"];
    
    [_heightsDic setValue:height forKey:[NSString stringWithFormat:@"%ld",(long)indexPath.row]];
}

#pragma mark -- lazyLoad

-(UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height-64) style:UITableViewStylePlain];
        _tableView.estimatedRowHeight = 100;
        _tableView.rowHeight = UITableViewAutomaticDimension;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        [_tableView registerClass:[BIZOfficialRapidConsultDetailQuestionCell class] forCellReuseIdentifier:kOfficialConsultQuestionCellID];
        [_tableView registerClass:[BIZOfficialAnswerCell class] forCellReuseIdentifier:kOfficialAnswerCellID];
        [_tableView registerClass:[BIZOfficialAppendQuestionCell class] forCellReuseIdentifier:kOfficialAppendQuestionCellID];
        
        _tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            [self requestOfficialConsultDetail];
        }];

    }
    return _tableView;
}

@end
